package com.android.superobstacleavoider;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;


public class Triangle {

	protected FloatBuffer m_vertexBuffer;
	
	protected FloatBuffer m_colorBuffer;
	
	protected float m_angle = 0.0f;
	
	/** The initial vertex definition */
	protected float m_vertices[] = 
	{ 
		0.0f, 1.0f, 0.0f, 	//Top
		-1.0f, -1.0f, 0.0f, //Bottom Left
		1.0f, -1.0f, 0.0f 	//Bottom Right
	};
	
	/** The initial color definition */
	protected float m_colors[] = 
	{ 
		1.0f, 0.0f, 0.0f, 1.0f, 	//Top
		0.0f, 1.0f, 0.0f, 1.0f, 	//Bottom Left
		0.0f, 0.0f, 1.0f, 1.0f, 	//Bottom Right
	};
	
	/**
	 * The Triangle constructor.
	 * 
	 * Initiate the buffers.
	 */
	public Triangle() 
	{
		//
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(m_colors.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		m_vertexBuffer = byteBuffer.asFloatBuffer();
		m_vertexBuffer.put(m_vertices);
		m_vertexBuffer.position(0);
		
		//colors
		byteBuffer = ByteBuffer.allocateDirect(m_colors.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		m_colorBuffer = byteBuffer.asFloatBuffer();
		m_colorBuffer.put(m_colors);
		m_colorBuffer.position(0);
		
		
	}
	
	/**
	 * The object own drawing function.
	 * Called from the renderer to redraw this instance
	 * with possible changes in values.
	 * 
	 * @param gl - The GL context
	 */
	public void draw(GL10 gl) 
	{
		//sets front face to counter clockwise
		gl.glFrontFace(GL10.GL_CCW);
		
		
		//point to our vertexbuffer and color buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_vertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, m_colorBuffer);
		
		//enable vertexbuffer and colorbuffer
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		
		//draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, m_vertices.length / 3);
		
		//Disable the client states before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}

	public float getAngle() {
		return m_angle;
	}

	public void setAngle(float angle) {
		m_angle = angle;
	}

	public FloatBuffer getVertexBuffer() {
		return m_vertexBuffer;
	}

	public void setVertexBuffer(FloatBuffer a_vertexBuffer) {
		this.m_vertexBuffer = a_vertexBuffer;
	}

	public FloatBuffer getColorBuffer() {
		return m_colorBuffer;
	}

	public void setColorBuffer(FloatBuffer a_colorBuffer) {
		this.m_colorBuffer = a_colorBuffer;
	}

	public float[] getVertices() {
		return m_vertices;
	}

	public void setVertices(float[] a_vertices) {
		this.m_vertices = a_vertices;
	}

	public float[] getColors() {
		return m_colors;
	}

	public void setColors(float[] a_colors) {
		this.m_colors = a_colors;
	}
}
